package com.miao.hong.fang.course;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionFormClass;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.validator.DynaValidatorActionForm;

import com.miao.hong.fang.IConstants;
import com.miao.hong.fang.admin.service.AdminService;
import com.miao.hong.fang.course.service.CourseService;
import com.miao.hong.fang.index.service.IndexService;
import com.miao.hong.fang.user.service.UserService;
import com.miao.hong.fang.util.SpringBeansHolder;
import com.miao.hong.fang.util.Utils;
import com.miao.hong.fang.util.pagination.ListAndCount;
import com.miao.hong.fang.util.pagination.PaginationContext;

public class CourseAction extends DispatchAction{

	private CourseService courseService = SpringBeansHolder.getInstance().getCourseService();
	private AdminService adminService = SpringBeansHolder.getInstance().getAdminService();
	private IndexService indexService = SpringBeansHolder.getInstance().getIndexService();
	private UserService userService = SpringBeansHolder.getInstance().getUserService();
	
	public ActionForward viewCourseDetail(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) {
		
		String courseId = request.getParameter("courseId");
		
		CourseTO course = adminService.getCourseDetail(courseId);
		String teacherName = adminService.getTeacherDetail(course.getCourseTeacher()).getTeacherName();
		request.setAttribute("course", course);
		request.setAttribute("teacherName", teacherName);
		
		List hotCouse = indexService.getHotCourse();
		request.setAttribute("hotCourse", Utils.getCellOfList(hotCouse, 10, IConstants.ORDER_TYPE_FRONT));
		
		String userId = (String) request.getSession().getAttribute("userId");
		boolean hasCollected = false;
		if(userId != null) {
			hasCollected = userService.checkUserCourse(userId, courseId);			
		}
		request.setAttribute("hasCollected", String.valueOf(hasCollected));
		
		return mapping.findForward("success");
	}
	
	public ActionForward gotoPublicCourse(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws UnsupportedEncodingException {
		
		String location = request.getParameter("location");
		if(location != null) {
			location = java.net.URLDecoder.decode(location, "UTF-8");
		}
		
		String category = request.getParameter("category");
		if(category != null) {
			category = java.net.URLDecoder.decode(category, "UTF-8");
		}
		String year = request.getParameter("year");
		String month = request.getParameter("month");
		
		List courseCategoryList = courseService.getAllCourseCategory();
		ListAndCount publicCourse = courseService.getPublicCourse(year, month, location, category);
		List courseCities = courseService.getAllCourseCities();
		
		request.setAttribute("publicCourse", publicCourse.getList());
		request.setAttribute("totalCount", String.valueOf(publicCourse.getCount()));
		
		request.setAttribute("courseCities", courseCities);
		request.setAttribute("courseCategory", courseCategoryList);
		
		request.setAttribute("location", location);
		request.setAttribute("category", category);
		request.setAttribute("month", month);
		
		DynaActionFormClass dynaClass = DynaActionFormClass.
							createDynaActionFormClass(mapping.getModuleConfig().findFormBeanConfig("forHiddenCriteriaForm"));
		DynaValidatorActionForm dynaForm = new DynaValidatorActionForm();
		try {
			dynaForm = (DynaValidatorActionForm)dynaClass.newInstance();
		}catch (Exception e) {
			e.printStackTrace();
		}
		dynaForm.set("month", month);
		dynaForm.set("location", location);
		dynaForm.set("category", category);
		request.setAttribute("forHiddenCriteriaForm", dynaForm);

		request.setAttribute("paginationSortOrderData", PaginationContext.getPaginationSortOrderData());
		
		List hotCouse = indexService.getHotCourse();
		request.setAttribute("hotCourse", Utils.getCellOfList(hotCouse, 10, IConstants.ORDER_TYPE_FRONT));
		
		return mapping.findForward("success");
	}
}
